<?php

$dir = pathinfo($_SERVER['SCRIPT_FILENAME']);

if (ini_set("include_path", ini_get("include_path") . ((strtoupper(PHP_OS) == "LINUX") ? ":" : ";") . $dir['dirname']) === false) {
    echo "ERROR: Definicion en el include_path";
    exit;
}

include('../adodb/adodb.inc.php');
include_once('../adodb/adodb-pager.inc.php');
include_once('../conf/dbconn_param.php');

$db = ADONewConnection($dbdriver);
$db->Connect($server, $user, $password, $database);
$db->SetFetchMode(ADODB_FETCH_ASSOC);



$packing = $_REQUEST['packing'];
$page = $_REQUEST['page']; // get the requested page
$limit = $_REQUEST['rows']; // get how many rows we want to have into the grid
$sidx = $_REQUEST['sidx']; // get index row - i.e. user click to sort
$sord = $_REQUEST['sord']; // get the direction





$db->debug = false;

$sql = "SELECT  pac.prep_id,
	pac.orden,
	pac.packing,
	pac.marca,
	pac.tipo_caja,
	pac.caja,
	pro.nombre AS producto,
	var.nombre AS variedad,
	pac.longitud,
	bod.nombre AS bodega,
	pac.tallos AS tallosBunche,
	pac.bunches AS bunchesCaja,
	(pac.bunches * pac.tallos) AS tallos,
	0 AS precioFob,
	0 AS precioCif,
	0 AS totalFob,
	0 AS totalCif
	FROM `prepacking` pac INNER JOIN `producto` pro ON pac.producto_id = pro.producto_id
	INNER JOIN variedad var ON var.variedad_id = pac.variedad_id 
	INNER JOIN bodega bod ON bod.bodega_id = pac.bodega_id
	where pac.packing = $packing
	ORDER BY pac.prep_id";

//echo $sql;

$rs = $db->Execute($sql);
$datos = $rs->GetRows();

$count = count($datos);

if ($count > 0) {
    $total_pages = ceil($count / $limit);
} else {
    $total_pages = 0;
}
if ($page > $total_pages)
    $page = $total_pages;



$total_pages = count($datos);


$responce->page = $page;
$responce->total = $total_pages;
$responce->records = $count;
$i = 0;
foreach ($datos as $row) {
    $responce->rows[$i]['id'] = $row["prep_id"];
    $responce->rows[$i]['cell'] = array($row[prep_id],
       // $row[orden],
        //$row[packing],
        $row[marca],
        $row[tipo_caja],
        $row[caja],
        $row[producto],
        $row[variedad],
        $row[longitud],
        $row[bodega],
        $row[tallosBunche],
        $row[bunchesCaja],
        $row[tallos],
        $row[precioFob],
        $row[precioCif],
        $row[totalFob],
        $row[totalCif]);

    $i++;
}

echo json_encode($responce);



$db->Close;
?>	